<template>
	<!-- 头部 -->
	<view>
		<view style="display: flex;justify-content: space-between;">
			<view style="margin: 15px 15px 5px 15px;font-weight: 600;font-size: 15px;color: #25406e">
				<text>现场协调</text>
			</view>
			<view style="margin: 15px 15px 5px 15px;">
				<selectSwitch :switchList="switchList" @change="changeSwitch" />
			</view>
		</view>
		<text style="margin-left: 15px;font-size: 10px;display: flex;color: #a7a7a7;font-weight: 600;">今日客流最高分<text
				style="color: #515a9c">14:00-15:00</text> 共202人</text>
		<view class="charts-box">
			<qiun-data-charts type="line" :opts="opts" :chartData="chartData" :ontouch="true" />
		</view>
	</view>

</template>

<script>
	import {
		findShopStatisticsTrend
	} from '@/network/trafficBriefing.js';
	import {
		color
	} from 'echarts';
	import selectSwitch from "@/components/xuan-switch/xuan-switch.vue";
	export default {
		components: {
			selectSwitch
		},
		data() {
			return {
				current: {
					organId: uni.getStorageSync('organId'),
				},
				traversalTime: new Date().toISOString().slice(0, 10),
				chartData: {},
				time: [],
				today: [],
				yesterday: [],
				blueData: "今日",
				redData: "昨日",
				switchList: ["今日", "本月"],
				//您可以通过修改 config-ucharts.js 文件中下标为 ['line'] 的节点来配置全局默认参数，如都是默认参数，此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
				opts: {
					color: ["#1890FF", "#EE6666"],
					padding: [15, 0, 0, 0],
					enableScroll: true,
					legend: {
						icon: 'circle',
						show: true,
						position: 'top',
						float: 'right'

					},
					xAxis: {
						disableGrid: true,
						scrollShow: true,
						itemCount: 4
					},
					yAxis: {
						gridType: "dash",
						dashLength: 2
					},
					extra: {
						line: {
							type: "straight",
							width: 2,
							activeType: "hollow"
						}
					}
				}
			};
		},
		onReady() {
			this.current.currentDate = "1"
			this.getServerData();
		},
		methods: {
			getServerData() {
				const arr = []
				const arr1 = []
				findShopStatisticsTrend(this.current).then(res => {
					const code = res.data.code;
					if (code === 200) {
						this.briefingData = res.data.result
						// console.log(this.traversalTime)
						this.briefingData.forEach((item, index) => {
							if (this.traversalTime == item.statisticsDate) {
								arr.push(item)
								this.time.push(item.time)
							} else {
								arr1.push(item)
							}
						})
						arr.forEach((item, index) => {
							this.today.push(item.totalPassengerFlow)
						})
						arr1.forEach((item, index) => {
							this.yesterday.push(item.totalPassengerFlow)
						})
					} else {
						uni.showToast({
							title: res.data.message
						})
					}
				}).catch(err => {
					// console.log(err)
				})

				//模拟从服务器获取数据时的延时
				setTimeout(() => {
					//模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
					let res = {
						categories: this.time,
						series: [{
								name: this.blueData,
								data: this.today
							},
							{
								name: this.redData,
								data: this.yesterday
							},
						]
					};
					this.chartData = JSON.parse(JSON.stringify(res));
				}, 500);
			},
			clear() {
				this.time = []
				this.today = []
				this.yesterday = []
				this.current = {
					organId: uni.getStorageSync('organId'),
				}
			},
			changeSwitch(isSwitch) { // isSwitch：当前开关状态
				console.log(isSwitch);
				if (isSwitch == true) {
					this.clear()
					this.blueData = "今日"
					this.redData = "昨日"
					this.current.currentDate = "1"
					this.getServerData();
				}
				if (isSwitch == false) {
					this.clear()
					this.blueData = "本月"
					this.redData = "上月"
					this.current.monthlyDate = this.traversalTime
					this.getServerData();
				}
			}

		}
	};
</script>

<style scoped>
	/* 请根据实际需求修改父元素尺寸，组件自动识别宽高 */
	.charts-box {
		width: 100%;
	}

	.switch {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		width: 120px;
		height: 54rpx;
		border-radius: 54rpx;
		background: #f7f7fb;
	}

	.switchNums {
		width: 112rpx;
		height: 54rpx;
		line-height: 54rpx;
		border-radius: 54rpx;
		text-align: center;
		color: #C1C1C1;
		font-size: 28rpx;
		font-weight: bold;
	}

	.currentNum {
		background: #6a74c9;
		color: #ffffff;
	}
</style>